<?php

/*
 * This file is part of the Geek-Zoo Projects.
 *
 * @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
 * @author quqiang <quqiang@geek-zoo.com>
 *
 */

class Action extends admin_abs
{   
    function index()
    {
		$data = _model('item')->getList("WHERE status!=3 ORDER BY id DESC", new Pager(20));
        Smarty3::instance()->assign($data);
        Smarty3::instance()->display('item_list.html');
    }

	function update()
	{
		$id = _GET('id', 0);
		if (!$id) {
			msg('参数错误');
		}
		$item = _model('item')->read(array('id'=>$id));

		if ($item['type'] == 1) {
			$item['data'] = unserialize($item['data']);
		}
		
		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
			$info = _POST('info', array());
			$type_data_video = _POST('type_data_video');
			$type_data_img = _POST('type_data_img');
			$item_ralation = array_filter(_POST('item_ralation', array()));
			
			if (empty($info['title'])) {
				msg('标题不能为空');
			}
			if ($info['type'] == 0 && empty($type_data_img)) {
				msg('图片资源必须上传');
			}
			if ($info['type'] == 1 && empty($type_data_video)) {
				msg('视频信息必须填写');
			}
			
			if ($info['type'] == 1) {
				// $info['data'] = $type_data_video;
				$a = video_getflash($type_data_video, 'youku.com');
				$b = video_getflashinfo($type_data_video,'youku.com');
				if (!$b) {
					msg('视频解析错误');
				}
				$video_data = array(
					'flashvar'=>$a,
					'flashimg'=>$b['img'],
					'host'=>'youku.com',
					'source'=>$type_data_video,
					'title'=>$b['title']
				);
				$info['data'] = serialize($video_data);
			}
			
			if ($info['type'] == 0) {
				$info['data'] = $type_data_img;
			}
			
			//添加附件
			$check = array_filter($_FILES['attr']['tmp_name']);
			$attr_size = _POST('attr_size', array());
			if (!empty($check)) {
				$urls = array();
				$files = Uploadr::farmat_file($_FILES['attr']);
				foreach ($files as $key => $value) {
					$url = Uploadr::saveFILES($value, dirname(ROOT_PATH).'/attachments');
					$urls[$key] = $url;
				}
				if ($urls) {
					foreach ($urls as  $key=>$val) {
						_model('item_attachments')->create(array('item_id'=>$id, 'size_id'=>$attr_size[$key], 'data'=>$val, 'add_time'=>time()));
					}
				}
			}
			//
			
			
			_model('item')->update(array('id'=>$id), $info);
			_model('item_ralation')->delete(array('item_id'=>$id));
			if ($item_ralation) {
				foreach ($item_ralation as $key => $value) {
					_model('item_ralation')->create(array('item_id'=>$id, 'category_id'=>$value));
				}
			}
			msg('更新成功', '?url=item');

		} else {
			$cate_gory = _model('category')->getList();
			$item_ralation = _model('item_ralation')->getList(" WHERE item_id=$id ");
			$item_ralation_new = array();
			foreach ($item_ralation as $key => $value) {
				$item_ralation_new[] = $value['category_id'];
			}
			$item_att = _model('item_attachments')->getList(array('item_id'=>$id));
			$size = _model('phone_size')->getList();
			Smarty3::instance()->assign('size', $size);
			Smarty3::instance()->assign('item_att', $item_att);
			Smarty3::instance()->assign('item_ralation', $item_ralation_new);
			Smarty3::instance()->assign('cate_gory', $cate_gory);
			Smarty3::instance()->assign('item', $item);
			Smarty3::instance()->display('item_update.html');
		}
		
	}
	
	function delete()
	{		
		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
			$check = _POST('check', array());
			if ($check) {
				foreach ($check as $key => $value) {
					_model('item')->update(array('id'=>$value), array('status'=>'3'));
				}
			}
			redirect('?url=item');
	        
		} else {
			$id = _GET('id', 0);
			if (!$id) {
				msg('参数错误');
			}
			_model('item')->update(array('id'=>$id), array('status'=>'3'));
			redirect('?url=item');
		}
	}
	
	function shenhe()
	{
		$id = _GET('id', 0);
		if (!$id) {
			die('fail');
		}
		$item = _model('item')->read(array('id'=>$id));
		if ($item['status'] == 0) {
			_model('item')->update(array('id'=>$id), array('status'=>1));
			die('1');
		} else {
			_model('item')->update(array('id'=>$id), array('status'=>0));
			die('0');
		}
	}


	function tuijian()
	{
		$id = _GET('id', 0);
		if (!$id) {
			die('fail');
		}
		$item = _model('item')->read(array('id'=>$id));
		if ($item['is_recommend'] == 0) {
			_model('item')->update(array('id'=>$id), array('is_recommend'=>1));
			die('1');
		} else {
			_model('item')->update(array('id'=>$id), array('is_recommend'=>0));
			die('0');
		}
	}
	
	function delete_attr()
	{
		$id = _GET('id', 0);
		if ($id) {
			_model('item_attachments')->delete(array('id'=>$id));
		}
		echo '1';
	}
}


?>